package se.sudoku;

import se.sudoku.SudokuView;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class SudokuAlpha extends Activity {
	//MAIN MENU ITEMS
	private PopupWindow mainmenu;
	private Button mainmenu_btn_single, mainmenu_btn_multi, mainmenu_btn_quit, mainmenu_btn_options, mainmenu_btn_tutorial;
	private PopupWindow solomenu;
	private Button solomenu_btn_start, solomenu_btn_back;
	private PopupWindow multimenu;
	private Button multimenu_btn_create, multimenu_btn_join, multimenu_btn_back;
	private PopupWindow createmenu;
	private Button createmenu_btn_normal, createmenu_btn_battle, createmenu_btn_coop, createmenu_btn_back;
	private PopupWindow joinmenu;
	private Button joinmenu_btn_back;
	private PopupWindow tutorialmenu;
	private Button tutorialmenu_btn_back;
	private PopupWindow optionsmenu;
	private Button optionsmenu_btn_back;
	
	
	//PAUSE MENU ITEMS
	private PopupWindow pausemenu;
	private Button pausemenu_btn_resume, pausemenu_btn_mainmenu, pausemenu_btn_quit;
	
	//DIGITHOLDER ITEMS
	private PopupWindow digitPopup;
	private Button digit_1, digit_2, digit_3, digit_4, digit_5, digit_6, digit_7, digit_8, digit_9, digit_blank;
	private LinearLayout digitHolder;
	private Button chosenDigit;
	
	//GAME ITEMS
	Boolean GAME_ON;
	View gamefield;
	SudokuView sudoku;
	public final String easySudoku =
		"360000000004230800000004200" +
		"070460003820000014500013020" +
		"001900000007048300000000045" ;
	private int puzzle[] = new int[9*9];
	private int used[] = new int[9*9];
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainmenubg);
	
		//create the menus
		createMainMenu();
		createSoloMenu();

		createPauseMenu();
		createDigitHolder();

		createMultiMenu();
		createCreateMenu();
		createJoinMenu();
		createOptionsMenu();
		createTutorialMenu();
		

		
		//no game running yet

		GAME_ON = false;
		gamefield = getLayoutInflater().inflate(R.layout.game, null);
		sudoku = (SudokuView)gamefield.findViewById(R.id.sudokuview);
		puzzle = fromStringToPuzzle(easySudoku);
		used = fromStringToPuzzle(easySudoku);
		
	}

	
	@Override
	public void onWindowFocusChanged(boolean x)
	{
		super.onWindowFocusChanged(x);
		toggleWindow(mainmenu);
	}
	
	
	
	
	// CREATE MAIN MENU
	public void createMainMenu(){
		mainmenu = new PopupWindow(View.inflate(this, R.layout.mainmenu, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, false);	    
		//buttons here
		mainmenu_btn_single = (Button)mainmenu.getContentView().findViewById(R.id.mainmenu_btn_single);
	    mainmenu_btn_single.setOnClickListener(mainmenuListener);
		mainmenu_btn_multi = (Button)mainmenu.getContentView().findViewById(R.id.mainmenu_btn_multi);
		mainmenu_btn_multi.setOnClickListener(mainmenuListener);
	    mainmenu_btn_options = (Button)mainmenu.getContentView().findViewById(R.id.mainmenu_btn_options);
	    mainmenu_btn_options.setOnClickListener(mainmenuListener);
	    mainmenu_btn_tutorial = (Button)mainmenu.getContentView().findViewById(R.id.mainmenu_btn_tutorial);
	    mainmenu_btn_tutorial.setOnClickListener(mainmenuListener);
	    mainmenu_btn_quit = (Button)mainmenu.getContentView().findViewById(R.id.mainmenu_btn_quit);
	    mainmenu_btn_quit.setOnClickListener(mainmenuListener);
	}
	// CREATE SOLO SUBMENU
	public void createSoloMenu(){
		solomenu = new PopupWindow(View.inflate(this, R.layout.solomenu, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, false);
	    
		//buttons here
		solomenu_btn_start = (Button)solomenu.getContentView().findViewById(R.id.solomenu_btn_start);
	    solomenu_btn_start.setOnClickListener(mainmenuListener);
		solomenu_btn_back = (Button)solomenu.getContentView().findViewById(R.id.solomenu_btn_back);
		solomenu_btn_back.setOnClickListener(mainmenuListener);
	}
	// CREATE MULTI SUBMENU
	public void createMultiMenu(){
		multimenu = new PopupWindow(View.inflate(this, R.layout.multimenu, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, false);
	    
		//buttons here
		multimenu_btn_create = (Button)multimenu.getContentView().findViewById(R.id.multimenu_btn_create);
		multimenu_btn_create.setOnClickListener(mainmenuListener);
		multimenu_btn_join = (Button)multimenu.getContentView().findViewById(R.id.multimenu_btn_join);
		multimenu_btn_join.setOnClickListener(mainmenuListener);
		multimenu_btn_back = (Button)multimenu.getContentView().findViewById(R.id.multimenu_btn_back);
		multimenu_btn_back.setOnClickListener(mainmenuListener);
	}
	// CREATE MULTI->CREATE SUBMENU
	public void createCreateMenu(){
		createmenu = new PopupWindow(View.inflate(this, R.layout.createmenu, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, false);
	    
		//buttons here
		createmenu_btn_normal = (Button)createmenu.getContentView().findViewById(R.id.createmenu_btn_normal);
		createmenu_btn_normal.setOnClickListener(mainmenuListener);
		createmenu_btn_battle = (Button)createmenu.getContentView().findViewById(R.id.createmenu_btn_battle);
		createmenu_btn_battle.setOnClickListener(mainmenuListener);
		createmenu_btn_coop = (Button)createmenu.getContentView().findViewById(R.id.createmenu_btn_coop);
		createmenu_btn_coop.setOnClickListener(mainmenuListener);
		createmenu_btn_back = (Button)createmenu.getContentView().findViewById(R.id.createmenu_btn_back);
		createmenu_btn_back.setOnClickListener(mainmenuListener);
	}
	// CREATE MULTI->JOIN SUBMENU
	public void createJoinMenu(){
		joinmenu = new PopupWindow(View.inflate(this, R.layout.joinmenu, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, false);
	    
		//buttons here
		joinmenu_btn_back = (Button)joinmenu.getContentView().findViewById(R.id.joinmenu_btn_back);
		joinmenu_btn_back.setOnClickListener(mainmenuListener);
	}
	
	// CREATE OPTIONS SUBMENU
	public void createOptionsMenu(){
		optionsmenu = new PopupWindow(View.inflate(this, R.layout.optionsmenu, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, false);
	    
		//buttons here
		optionsmenu_btn_back = (Button)optionsmenu.getContentView().findViewById(R.id.optionsmenu_btn_back);
		optionsmenu_btn_back.setOnClickListener(mainmenuListener);
	}
	
	// CREATE TUTORIAL SUBMENU
	public void createTutorialMenu(){
		tutorialmenu = new PopupWindow(View.inflate(this, R.layout.tutorialmenu, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, false);
	    
		//buttons here
		tutorialmenu_btn_back = (Button)tutorialmenu.getContentView().findViewById(R.id.tutorialmenu_btn_back);
		tutorialmenu_btn_back.setOnClickListener(mainmenuListener);
	}
	
	
	//CREATE PAUSE MENU 
	public void createPauseMenu(){
		pausemenu = new PopupWindow(View.inflate(this, R.layout.pausemenu, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, false);    
		
		//buttons here
		pausemenu_btn_resume = (Button)pausemenu.getContentView().findViewById(R.id.pausemenu_btn_resume);
		pausemenu_btn_resume.setOnClickListener(pausemenuListener);
	    pausemenu_btn_mainmenu = (Button)pausemenu.getContentView().findViewById(R.id.pausemenu_btn_mainmenu);
	    pausemenu_btn_mainmenu.setOnClickListener(pausemenuListener);
	    pausemenu_btn_quit = (Button)pausemenu.getContentView().findViewById(R.id.pausemenu_btn_quit);   
	    pausemenu_btn_quit.setOnClickListener(pausemenuListener);
	    
	}
	//	CREATE DIGITHOLDER
	public void createDigitHolder(){
		digitPopup = new PopupWindow(View.inflate(this, R.layout.digitholder, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, false);
        digitHolder = (LinearLayout)digitPopup.getContentView().findViewById(R.id.digitholder);
        digitHolder.setOnClickListener(digitHolderListener);
        digit_1 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_1);
        digit_1.setOnClickListener(digitHolderListener);
        digit_2 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_2);
        digit_2.setOnClickListener(digitHolderListener);
        digit_3 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_3);
        digit_3.setOnClickListener(digitHolderListener);
        digit_4 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_4);
        digit_4.setOnClickListener(digitHolderListener);
        digit_5 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_5);
        digit_5.setOnClickListener(digitHolderListener);
        digit_6 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_6);
        digit_6.setOnClickListener(digitHolderListener);
        digit_7 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_7);
        digit_7.setOnClickListener(digitHolderListener);
        digit_8 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_8);
        digit_8.setOnClickListener(digitHolderListener);
        digit_9 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_9);
        digit_9.setOnClickListener(digitHolderListener);
        digit_blank = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_blank);
        digit_blank.setOnClickListener(digitHolderListener);
	}
	
	//MAIN MENU CLICK LISTENERS
	private OnClickListener mainmenuListener = new OnClickListener() {
		public void onClick(View v) {
	    	//SOLO
	    	if(v==mainmenu_btn_single){
	    		toggleWindow(mainmenu);
	    		toggleWindow(solomenu);
	    	}
	    	//MAIN->SOLO BUTTONS
	    	if(v==solomenu_btn_start){
	    		toggleWindow(solomenu);
	    		setContentView(gamefield);
	    	    GAME_ON = true;
	    	}
	    	if(v==solomenu_btn_back){
	    		toggleWindow(solomenu);
	    		toggleWindow(mainmenu);
	    	}
	    		    	
	    	//MULTI
	    	if(v==mainmenu_btn_multi){
	    		toggleWindow(mainmenu);
	    		toggleWindow(multimenu);
	    	}
	    	//MAIN->MULTI SUB MENU BUTTONS
	    	if(v==multimenu_btn_create){
	    		toggleWindow(multimenu);
	    		toggleWindow(createmenu);
	    	}
	    	if(v==multimenu_btn_join){
	    		toggleWindow(multimenu);
	    		toggleWindow(joinmenu);
	    	}
	    	if(v==multimenu_btn_back){
	    		toggleWindow(multimenu);
	    		toggleWindow(mainmenu);
	    	}
	    	//MAIN->MULTI->CREATE SUB MENU BUTTONS
	    	if(v==createmenu_btn_normal){
	    		
	    	}
	    	if(v==createmenu_btn_battle){
	    		
	    	}
	    	if(v==createmenu_btn_coop){
	    		
	    	}
	    	if(v==createmenu_btn_back){
	    		toggleWindow(createmenu);
	    		toggleWindow(multimenu);
	    	}
	    	//MAIN->MULTI->JOIN SUB MENU BUTTONS
	    	if(v==joinmenu_btn_back){
	    		toggleWindow(joinmenu);
	    		toggleWindow(multimenu);
	    	} 
	    	
	    	//OPTIONS
	    	if(v==mainmenu_btn_options){
	    		toggleWindow(mainmenu);
	    		toggleWindow(optionsmenu);
	    	}
	    	//MAIN->OPTIONS SUB MENU BUTTONS
	    	if(v==optionsmenu_btn_back){
	    		toggleWindow(optionsmenu);
	    		toggleWindow(mainmenu);
	    	}
	    	
	    	//TUTORIAL
	    	if(v==mainmenu_btn_tutorial){
	    		toggleWindow(mainmenu);
	    		toggleWindow(tutorialmenu);
	    	}
	    	//MAIN->TUTORIAL SUB MENU BUTTONS
	    	if(v==tutorialmenu_btn_back){
	    		toggleWindow(tutorialmenu);
	    		toggleWindow(mainmenu);
	    	}
	    	
	    	//QUIT
	    	if(v==mainmenu_btn_quit){
	    		//toggleWindow(mainmenu);
	    		finish();
	    	}	
	    }
	};

	//PAUSE MENU CLICK LISTENERS
	private OnClickListener pausemenuListener = new OnClickListener() {
	    public void onClick(View v) {
	    	if(v==pausemenu_btn_resume){
	    		toggleWindow(pausemenu);
	    		//toggleWindow(singlemenu);
	    	}
	    	if(v==pausemenu_btn_mainmenu){
	    		toggleWindow(pausemenu);
	    		toggleWindow(mainmenu);
	    		setContentView(R.layout.mainmenubg);
	    		GAME_ON = false;
	    	}
	    	if(v==pausemenu_btn_quit){
	    		//toggleWindow(mainmenu);
	    		finish();
	    	}	
	    }
	};
	
	//DIGITHOLDER LISTERNERS
	private OnClickListener digitHolderListener = new OnClickListener() {            
	    public void onClick(View v){
	    	//insert number
	    	if(v.getParent().getParent()==digitHolder){
	    		chosenDigit = (Button)v;
	    		sudoku.setSelectedTile(Integer.parseInt(chosenDigit.getText().toString()));
	    		toggleWindow(digitPopup);
	    	}
	    	//remove inserted number
	    	else if(v==digit_blank){
	    		sudoku.setSelectedTile(0);
	    		toggleWindow(digitPopup);
	    	}
	    	//remove popup by pressing the outside
	    	else if(v==digitHolder){
	    		toggleWindow(digitPopup);
	    	}
	    }
	};


	public boolean onKeyDown(int keyCode, KeyEvent event) { 
	                
	     switch (keyCode) { 
	     case KeyEvent.KEYCODE_MENU: 
	          if(GAME_ON){
	        	  toggleWindow(pausemenu); 
	        	  return true;
	          }
	     } 
	      
	     return false; 
	}
   
    public void toggleDigits()
    {
    	toggleWindow(digitPopup);
    }
    
    public void toggleWindow(PopupWindow win){  	
    	if(!win.isShowing()){
    		if(GAME_ON){win.showAtLocation(gamefield, Gravity.CENTER, 0, 0);}
    		else{win.showAtLocation(this.findViewById(R.id.main_layout), Gravity.CENTER, 0, 0);}
    	}
    	else{
    		win.dismiss();
    	}
    }
    protected boolean isTileValid(int x,int y)
    {
    	
    	if(used[y*9 + x]!=0)
    	{
    		return false;
    	}
    	
    	return true;
    }
    //Return the tile at the given coordinates
    private int getTile(int x, int y) {
    	return puzzle[y * 9 + x];
    }
    
    //Change the tile at the given coordinates
    protected void setTile(int x, int y, int value) {
    	puzzle[y * 9 + x] = value;
    }
    
    //Convert an array into a puzzle string
    static private String toPuzzleString(int[] puz) {
	    StringBuilder buf = new StringBuilder();
	    for (int element : puz) {
	    	buf.append(element);
	    }
	    return buf.toString();
    }
    
    //Convert a puzzle string into an array
    static protected int[] fromStringToPuzzle(String string) {
	    int[] puz = new int[string.length()];
	    for (int i = 0; i < puz.length; i++) {
	    	puz[i] = string.charAt(i) - '0' ;
	    }
	    return puz;
    }
    // Return a string for the tile at the given coordinates
    protected String getTileString(int x, int y) {
	    int v = getTile(x, y);
	    if (v == 0)
	    	return "" ;
	    else
	    	return String.valueOf(v);
    }


}